/*
缓冲区模块,按CTCBuffer.h实现代码
*/
#include "CTCBuffer.h"
#include "Debug.h"
#include "CTCMsgV1.h"
#include <string.h> // 用于 memset 和 memcpy

static const uint32_t crc32_table[256] = {
    0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
    0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
    0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
    0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
    0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
    0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
    0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
    0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
    0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
    0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
    0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
    0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
    0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
    0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
    0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
    0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
    0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
    0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
    0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
    0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
    0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
    0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
    0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
    0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
    0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
    0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
    0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
    0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
    0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
    0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
    0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
    0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};



/// @brief 将字节数组指针前两个字节拼接成一个16位整数
uint16_t CTCBuffer_BytesToUint16(const uint8_t* data)
{
    return (data[0] << 8) | data[1];
}

/// @brief 将字节数组指针前四个字节拼接成一个32位整数
uint32_t CTCBuffer_BytesToUint32(const uint8_t* data)
{
    return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
}

/// @brief 填充uint16到字节数组指针前两个字节
void CTCBuffer_Uint16ToBytes(uint16_t val, uint8_t* data)
{
    data[0] = (val >> 8) & 0xFF; // 高字节在前
    data[1] = val & 0xFF;        // 低字节在后
}

/// @brief 填充uint32到字节数组指针前四个字节
void CTCBuffer_Uint32ToBytes(uint32_t val, uint8_t* data)
{
    data[0] = (val >> 24) & 0xFF; // 最高字节在前
    data[1] = (val >> 16) & 0xFF;
    data[2] = (val >> 8) & 0xFF;
    data[3] = val & 0xFF;         // 最低字节在后
}

/// @brief 初始化清空缓冲区
void CTCBuffer_Init(CTCBuffer *buf)
{
    buf->startpos = 0;
    buf->endpos   = 0;
    memset(buf->data, 0, CTCBUFFERLENGTH);
    buf->lock       = 0; // 初始化自旋锁为未锁定状态
    buf->readpos    = 0; // 初始化读取位置
}

/// @brief 自旋锁加锁
void CTCBuffer_Lock(CTCBuffer *buf)
{
    // 有可能死锁,暂时不使用
    // while (buf->lock) {
    //     // 自旋等待，直到锁被释放
    // }
    //buf->lock = 1; // 获取锁
}

/// @brief 自旋锁解锁
void CTCBuffer_Unlock(CTCBuffer *buf)
{
    //buf->lock = 0; // 释放锁
}

/// @brief 获取缓冲区有效数据长度
uint16_t CTCBuffer_DataLength(CTCBuffer *buf)
{
    uint16_t length;
    if (buf->endpos >= buf->startpos) {
        length = buf->endpos - buf->startpos;
    } else {
        length = CTCBUFFERLENGTH - (buf->startpos - buf->endpos);
    }
    return length;
}

/// @brief 获取当前可读取数据长度
/// @param buf 缓冲区对象
/// @return  返回缓冲区可读取数据长度
uint16_t CTCBuffer_CanReadDataLength(CTCBuffer *buf)
{
    uint16_t length;
    if (buf->endpos >= buf->readpos) {
        length = buf->endpos - buf->readpos;
    } else {
        length = CTCBUFFERLENGTH - (buf->readpos - buf->endpos);
    }
    return length;
}

/// @brief 获取缓冲区空闲长度
uint16_t CTCBuffer_FreeLength(CTCBuffer *buf)
{
    uint16_t free_length;
    free_length = CTCBUFFERLENGTH - CTCBuffer_DataLength(buf) - 1;
    return free_length;
}

/// @brief 从缓冲区读取2字节数字
CTC_BufferResult CTCBuffer_ReadUint16(CTCBuffer *buf, uint16_t *val)
{
    if (CTCBuffer_CanReadDataLength(buf) < 2) {
        return CTC_BUFFER_DATAMISSING; // 数据不足
    }

    *val         = (buf->data[buf->readpos] << 8) | buf->data[(buf->readpos + 1) % CTCBUFFERLENGTH];
    buf->readpos = (buf->readpos + 2) % CTCBUFFERLENGTH;
    return CTC_BUFFER_SUCCESS;
}

/// @brief 从缓冲区读取4字节数字
CTC_BufferResult CTCBuffer_ReadUint32(CTCBuffer *buf, uint32_t *val)
{
    if (CTCBuffer_CanReadDataLength(buf) < 4) {
        return CTC_BUFFER_DATAMISSING; // 数据不足
    }

    *val = (buf->data[buf->readpos] << 24) |
           (buf->data[(buf->readpos + 1) % CTCBUFFERLENGTH] << 16) |
           (buf->data[(buf->readpos + 2) % CTCBUFFERLENGTH] << 8) |
           buf->data[(buf->readpos + 3) % CTCBUFFERLENGTH];
    buf->readpos = (buf->readpos + 4) % CTCBUFFERLENGTH;

    return CTC_BUFFER_SUCCESS;
}

/// @brief 从缓冲区读取1字节数字
CTC_BufferResult CTCBuffer_ReadUint8(CTCBuffer *buf, uint8_t *val)
{
    if (CTCBuffer_CanReadDataLength(buf) < 1) {
        return CTC_BUFFER_DATAMISSING; // 数据不足
    }

    *val         = buf->data[buf->readpos];
    buf->readpos = (buf->readpos + 1) % CTCBUFFERLENGTH;
    return CTC_BUFFER_SUCCESS;
}

/// @brief 批量写入数据
CTC_BufferResult CTCBuffer_Write(CTCBuffer *buf, const uint8_t *data, uint8_t length)
{
    if (CTCBuffer_FreeLength(buf) < length) {
        return CTC_BUFFER_FULL;      // 缓冲区已满
    }

    for (size_t i = 0; i < length; i++) {
        buf->data[buf->endpos] = data[i];
        buf->endpos            = (buf->endpos + 1) % CTCBUFFERLENGTH;
    }
    return CTC_BUFFER_SUCCESS;
}

/// @brief 批量读取数据
CTC_BufferResult CTCBuffer_Read(CTCBuffer *buf, uint8_t *data, uint8_t length)
{
    if (CTCBuffer_CanReadDataLength(buf) < length) {
        return CTC_BUFFER_DATAMISSING; // 数据不足
    }

    for (size_t i = 0; i < length; i++) {
        data[i]      = buf->data[buf->readpos];
        buf->readpos = (buf->readpos + 1) % CTCBUFFERLENGTH;
    }
    return CTC_BUFFER_SUCCESS;
}

/// @brief 写入2字节数字
CTC_BufferResult CTCBuffer_WriteUint16(CTCBuffer *buf, uint16_t val)
{
    uint8_t data[2];
    CTCBuffer_Uint16ToBytes(val, data);
    return CTCBuffer_Write(buf, data, 2);
}

/// @brief 写入4字节数字
CTC_BufferResult CTCBuffer_WriteUint32(CTCBuffer *buf, uint32_t val)
{
    uint8_t data[4];
    CTCBuffer_Uint32ToBytes(val, data);
    return CTCBuffer_Write(buf, data, 4);
}

/// @brief 写入1字节数字
CTC_BufferResult CTCBuffer_WriteUint8(CTCBuffer *buf, uint8_t val)
{
    return CTCBuffer_Write(buf, &val, 1);
}

/// @brief 计算CRC32(国际算法)校验值
/// @param buf 缓冲区对象
/// @param startpos 起始位置
/// @param length 总长度
/// @return CRC32(国际算法)校验值
uint32_t CTCBuffer_CRC32(const CTCBuffer *buf, uint16_t startpos, uint16_t length)
{
    uint32_t crc = 0xFFFFFFFF; // 初始值
    uint16_t pos = startpos % CTCBUFFERLENGTH;

    for (uint16_t i = 0; i < length; i++) {
        uint8_t byte = buf->data[pos];
        crc          = (crc >> 8) ^ crc32_table[(crc ^ byte) & 0xFF];
        pos          = (pos + 1) % CTCBUFFERLENGTH;
    }

    return crc ^ 0xFFFFFFFF;
}

/// @brief 结束当前读取操作,更新有效数据起始位置
void CTCBuffer_EndRead(CTCBuffer *buf)
{
    buf->startpos = buf->readpos; // 更新有效数据起始位置
}

/// @brief 开始读取操作,保存当前读取位置
void CTCBuffer_StartRead(CTCBuffer *buf)
{
    buf->readpos = buf->startpos; // 保存当前读取位置
}

/// @brief 写入CTC消息
/// @param buf 缓冲对象
/// @param portid 端口ID,参考CTC_DEFALUT_PORTID
/// @param transactionId 事务ID
/// @param msgdata 消息数据指针
/// @param msglen 消息数据长度
/// @return 返回状态码,参考CTC_BufferStatus
CTC_BufferResult CTCBuffer_WriteMsgToSendBuffer(CTCBuffer *buf, uint8_t portid, uint16_t cmdid, uint32_t transactionId, const uint8_t *msgdata, uint8_t msglen)
{
    // 写入缓存的数据第一字节是帧的长度,接下来是帧的数据
    uint16_t frameLen = CTC_FRAME_OVERHEAD + msglen; // 总帧长度(不含数据部分)
    if (msglen > CTC_MAX_DATA_LEN) {
        DEBUG_ERROR("CTCBuffer_WriteMsg: Message length %d exceeds maximum %d\n", msglen, CTC_MAX_DATA_LEN);
        return CTC_BUFFER_OVERFLOW; // 数据长度超出限制
    }
    // 检查缓冲区是否有足够空间
    if (CTCBuffer_FreeLength(buf) < frameLen + 1) {
        DEBUG_WARN("CTCBuffer_WriteMsg: Not enough space in buffer. Required: %d, Available: %d\n", frameLen + 1, CTCBuffer_FreeLength(buf));
        return CTC_BUFFER_FULL; // 缓冲区空间不足
    }
    CTCBuffer_WriteUint8(buf, 0);                  // 预留发送次数位置
    CTCBuffer_WriteUint8(buf, (frameLen & 0xff)); // 写入帧长度
    uint16_t startpos = buf->endpos;              // 记录起始写入位置
    // 构造基础报文并写入缓冲区
    CTCBuffer_WriteUint16(buf, CTC_HEADER);    // 写入包头
    CTCBuffer_WriteUint8(buf, portid);         // 写入P位
    CTCBuffer_WriteUint16(buf, cmdid);         // 写入命令ID
    CTCBuffer_WriteUint32(buf, transactionId); // 写入事务ID
    CTCBuffer_WriteUint8(buf, msglen);        // 写入数据长度
    if (msglen > 0 && msgdata != NULL) {
        CTCBuffer_Write(buf, msgdata, msglen); // 写入数据部分
    }
    uint32_t crc = CTCBuffer_CRC32(buf, startpos, frameLen - 4); // 计算CRC32
    CTCBuffer_WriteUint32(buf, crc);                             // 写入CRC32
    return CTC_BUFFER_SUCCESS;
}

/// @brief 读取写入的指令,用于发送
/// @param buf 缓冲对象
/// @param msgdata 消息数据指针
/// @param msglen 消息数据长度
/// @param sendtimes 发送次数指针
/// @return 返回状态码,参考CTC_BufferStatus
CTC_BufferResult CTCBuffer_ReadCommandFromSendBuffer(CTCBuffer *sendbuf, uint8_t *msgdata, uint8_t *length,uint8_t *sendtimes)
{
    if (CTCBuffer_DataLength(sendbuf) < 2) {
        return CTC_BUFFER_EMPTY; // 无数据可读
    }
    CTCBuffer_StartRead(sendbuf); // 开始读取
    CTCBuffer_ReadUint8(sendbuf, sendtimes); // 读取发送次数
    CTC_BufferResult res = CTCBuffer_ReadUint8(sendbuf, length);
    if (*length < CTC_FRAME_OVERHEAD) return CTC_BUFFER_EMPTY; // 长度不够
    res = CTCBuffer_Read(sendbuf, msgdata, *length);
    if (res != CTC_BUFFER_SUCCESS) {
        CTCBuffer_EndRead(sendbuf); // 结束读取
        DEBUG_ERROR("CTC Master ReadSendBuffer Data Error:%d\r\n", res);
        return res;
    }
    //增加发送次数
    sendbuf->data[(sendbuf->startpos) % CTCBUFFERLENGTH] += 1; // 发送次数+1
    return CTC_BUFFER_SUCCESS; // 返回成功
}

/// @brief 清除第一个要发送的指令
/// @param sendbuf 缓冲对象
/// @param msgdata 消息数据指针
/// @param msglen 消息数据长度
/// @return 返回状态码,参考CTC_BufferStatus
CTC_BufferResult CTCBuffer_DeleteFirstCommandFromSend(CTCBuffer* sendbuf,uint16_t cmdid,uint32_t transactionid)
{
    if (CTCBuffer_DataLength(sendbuf) < 2) {
        return CTC_BUFFER_EMPTY; // 无数据可读
    }
    CTCBuffer_StartRead(sendbuf); // 开始读取
    uint8_t sendtimes;
    CTCBuffer_ReadUint8(sendbuf, &sendtimes); // 读取发送次数
    uint8_t length;
    CTCBuffer_ReadUint8(sendbuf, &length);
    // 读取包头
    uint16_t framehead = 0;
    CTCBuffer_ReadUint16(sendbuf, &framehead);
    //读取P位
    uint8_t portid;
    CTCBuffer_ReadUint8(sendbuf, &portid);    
    // 读取命令ID
    uint16_t readcmdid;
    CTCBuffer_ReadUint16(sendbuf, &readcmdid);
    // 读取事务ID
    uint32_t readtransactionid;
    CTCBuffer_ReadUint32(sendbuf, &readtransactionid);
    if(readcmdid!=cmdid || readtransactionid!=transactionid){
        //不调用EndRead，保持读取位置不变
        DEBUG_ERROR("CTCBuffer_DeleteFirstCommand: Command ID or Transaction ID not match. Expected cmdid: %d, transactionid: %d; Got cmdid: %d, transactionid: %d\n", cmdid, transactionid, readcmdid, readtransactionid);
        return CTC_BUFFER_CMDNOTMATCH; // 命令不匹配
    }
    // 丢弃剩余数据
    sendbuf->readpos = (sendbuf->readpos + length - 9) % CTCBUFFERLENGTH; // 减去已经读取的9字节
    CTCBuffer_EndRead(sendbuf); // 结束读取，更新起始位置
    return CTC_BUFFER_SUCCESS; // 返回成功
}

/// @brief 重置发送缓冲区的发送次数
/// @param sendbuf 缓冲对象
void CTCBuffer_ResetSendTimes(CTCBuffer* sendbuf)
{
    if (CTCBuffer_DataLength(sendbuf) < 2) {
        return; // 无数据可读
    }
    sendbuf->data[(sendbuf->startpos) % CTCBUFFERLENGTH] = 0; // 重置发送次数为0
}

/// @brief 使用CTC协议解析接收缓冲区数据
/// @param revicebuf 接收缓冲区对象
/// @param portid 端口ID指针
/// @param cmdid 命令ID指针
/// @param transactionid 事务ID指针
/// @param datafield 数据字段指针
/// @param datalen 数据长度指针
/// @return 返回状态码,参考CTC_BufferStatus
CTC_BufferResult CTCBuffer_ParseFromReceiveBuffer(CTCBuffer *revicebuf, uint8_t *portid, uint16_t *cmdid, uint32_t *transactionid, uint8_t *datafield, uint8_t *datalen)
{
    uint16_t bufferlength = CTCBuffer_DataLength(revicebuf);
    if(bufferlength==0) return CTC_BUFFER_EMPTY; // 无数据可读
    if (bufferlength < CTC_FRAME_OVERHEAD) {
        return CTC_BUFFER_PARSE_WAITMORE;
    }
    // 查找包头
    uint16_t framehead = 0;
    uint8_t bufferres  = 0;
    CTCBuffer_ReadUint16(revicebuf, &framehead);
    while (framehead != (uint16_t)CTC_HEADER) {
        // 包头不对,继续找包头
        CTCBuffer_EndRead(revicebuf); // 丢弃错误数据
        CTCBuffer_StartRead(revicebuf);
        bufferres = CTCBuffer_ReadUint16(revicebuf, &framehead);
        if (bufferres != CTC_BUFFER_SUCCESS) {
            // 找不到包头
            DEBUG_WARN("CTC Buffer Read Head Error,Clear Buffer\r\n");
            // 清空缓冲区
            CTCBuffer_Init(revicebuf);
            return CTC_BUFFER_PARSE_HEADNOTFOUND;
        }
    }
    // 找到了包头,继续读后续数据
    // 读取P位
    CTCBuffer_ReadUint8(revicebuf, portid);
    // 读取命令标识
    CTCBuffer_ReadUint16(revicebuf, cmdid);
    // 读取事务ID
    CTCBuffer_ReadUint32(revicebuf, transactionid);
    // 读取长度
    CTCBuffer_ReadUint8(revicebuf, datalen);
    // 判断剩余数据是否足够
    if (CTCBuffer_CanReadDataLength(revicebuf) < (*datalen + 4)) {
        // 数据不够,等待下次接收
        return CTC_BUFFER_PARSE_WAITMORE;
    }
    CTCBuffer_Read(revicebuf, datafield, *datalen); // 读出数据
    // 验证CRC32
    uint32_t crcdata = 0;
    CTCBuffer_ReadUint32(revicebuf, &crcdata);                                                            // 读出CRC32
    uint32_t crccom = CTCBuffer_CRC32(revicebuf, revicebuf->startpos, *datalen + CTC_FRAME_OVERHEAD - 4); // 计算CRC32
    CTCBuffer_EndRead(revicebuf);                                                                         // 结束读取
    if (crcdata != crccom) {
        // CRC32错误,丢弃此帧数据
        DEBUG_WARN("CTC Buffer CRC Error\r\n");
        return CTC_BUFFER_PARSE_CRCERROR;
    }
    return CTC_BUFFER_SUCCESS; // 返回成功
}

/// @brief 组装ACK数据,用于直接发送
/// @param buffer 缓冲字节数字指针
/// @param portid 端口ID
/// @param transactionId 事务ID
/// @param result 结果码,参考CTC_AckResult
void CTCBuffer_AssembleAckData(uint8_t* buffer, uint8_t portid,uint16_t cmdid, uint32_t transactionId, uint8_t result){
    // 构造基础报文并写入缓冲区用于直接发送
    CTCBuffer_Uint16ToBytes(CTC_HEADER, buffer); // 写入包头
    buffer[2] = portid;                      // 写入P位
    CTCBuffer_Uint16ToBytes(CTC_CMD_ACK, &buffer[3]); // 写入命令ID
    CTCBuffer_Uint32ToBytes(transactionId, &buffer[5]); // 写入事务ID
    buffer[9] = 3;                          // 写入数据长度
    CTCBuffer_Uint16ToBytes(cmdid, &buffer[10]); // 写入命令ID
    buffer[12] = (uint8_t)result;            // 写入结果码

    // 计算CRC32
    uint32_t crc = 0xFFFFFFFF; // 初始值
    for (uint16_t i = 0; i < CTC_ACKFRAME_LEN - 4; i++) {
        uint8_t byte = buffer[i];
        crc          = (crc >> 8) ^ crc32_table[(crc ^ byte) & 0xFF];
    }
    crc ^= 0xFFFFFFFF;
    
    // 写入CRC32四个字节
    CTCBuffer_Uint32ToBytes(crc, &buffer[CTC_ACKFRAME_LEN - 4]);
}

/// @brief 判断数据是否全等于0
/// @param data 数据指针
/// @param length 数据长度
/// @return 全等于0返回1,否则返回0
uint8_t CTCBuffer_IsAllZero(const uint8_t* data,uint8_t length)
{
    for(uint8_t i=0;i<length;i++){
        if(data[i]!=0) return 0; // 有不为0的字节
    }
    return 1; // 全部为0
}
